package com.huifaapp.mall.activity;

import android.annotation.TargetApi;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.huifaapp.mall.R;
import com.huifaapp.mall.adapter.TribeFragmentListviewAdapter;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.ClinimsgBean;
import com.huifaapp.mall.bean.TribeDetailsBean;
import com.huifaapp.mall.bean.WebJDShare;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.huifaapp.mall.uitils.MyListView;
import com.huifaapp.mall.uitils.UtilsImage;
import com.huifaapp.mall.view.CenterDialog;
import com.huifaapp.mall.view.CenterDialogTrue;
import com.huifaapp.mall.view.CenterDialogTrueIsYunDianZhu;
import com.huifaapp.mall.view.CenterDialogYouCode;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.socialize.utils.Log;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2017-11-27.
 */

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class TribeFragment extends Fragment implements CenterDialog.OnCenterItemClickListener ,
        CenterDialogTrue.OnCenterItemClickListener,CenterDialogTrueIsYunDianZhu.OnCenterItemClickListener,View.OnClickListener{
    CenterDialog centerDialog;
    CenterDialogYouCode centerDialogYouCode;
    CenterDialogTrue centerDialogTrue;
    CenterDialogTrueIsYunDianZhu centerDialogTrueIsYunDianZhu;
    private TextView tv_fragment_ruzhu;
    EditText et_tribe_phone;
    EditText et_tribe_weixinhao;
    EditText et_tribe_InvitateCode;
    CheckBox checkbox_tribe_Invitecode;
    private MyListView mylv_tribe_title;
    private ImageView  iv_tribe_image;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View viewLayout = inflater.inflate(R.layout.fragment_tribe, container, false);
        tv_fragment_ruzhu= (TextView) viewLayout.findViewById(R.id.tv_fragment_ruzhu);
        mylv_tribe_title= (MyListView) viewLayout.findViewById(R.id.mylv_tribe_title);
        iv_tribe_image= (ImageView) viewLayout.findViewById(R.id.iv_tribe_image);
        tv_fragment_ruzhu.setOnClickListener(this);
        centerDialog = new CenterDialog(getActivity(), R.layout.dialog_layout,
                new int[]{R.id.iv_cancel_dialog,R.id.et_tribe_phone, R.id.et_tribe_weixinhao,R.id.et_tribe_InvitateCode,R.id.checkbox_tribe_Invitecode,R.id.tv_tribe_true_submit});
        centerDialog.setOnCenterItemClickListener(this);

        centerDialogTrue = new CenterDialogTrue(getActivity(), R.layout.dialog_layout_true,
                new int[]{R.id.tv_true_tribe,R.id.tv_dialog_neirong});
        centerDialogTrue.setOnCenterItemClickListener(this);
        centerDialogTrueIsYunDianZhu = new CenterDialogTrueIsYunDianZhu(getActivity(), R.layout.dialog_layout_is_yundianzhu,
                new int[]{R.id.tv_true_tribe_yundianzhu});
        centerDialogTrueIsYunDianZhu.setOnCenterItemClickListener(this);
        Loadshendetails();
        if(SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN).equals("")){
            Intent intent=new Intent(getActivity(),LoginActivity.class);
            startActivity(intent);
        }else {
            Loadkaidian();
        }
//        Loadkaidian();
        return viewLayout;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_fragment_ruzhu:
                Loadkaidian();
//

                break;
        }
    }
    @Override
    public void OnCenterItemClick(CenterDialog dialog, View view) {
        et_tribe_phone= (EditText) dialog.findViewById(R.id.et_tribe_phone);
        et_tribe_weixinhao= (EditText) dialog.findViewById(R.id.et_tribe_weixinhao);
        et_tribe_InvitateCode= (EditText) dialog.findViewById(R.id.et_tribe_InvitateCode);
        checkbox_tribe_Invitecode= (CheckBox) dialog.findViewById(R.id.checkbox_tribe_Invitecode);
        if(checkbox_tribe_Invitecode.isChecked()) {
            et_tribe_InvitateCode.setVisibility(View.VISIBLE);

        }else{
            et_tribe_InvitateCode.setVisibility(View.GONE);

        }
        switch (view.getId()) {
            case R.id.tv_tribe_true_submit:
                Loadshenqing();
                break;
            case R.id.iv_cancel_dialog:
                centerDialog.dismiss();
                break;
        }


    }



    @Override
    public void OnCenterItemClick(CenterDialogTrue dialog, View view) {
        switch (view.getId()){
            case R.id.tv_true_tribe:
                centerDialogTrue.dismiss();
                break;
        }
    }
    @Override
    public void OnCenterItemClick(CenterDialogTrueIsYunDianZhu dialog, View view) {
        switch (view.getId()){
            case R.id.tv_true_tribe_yundianzhu:
                centerDialogTrueIsYunDianZhu.dismiss();
                break;
        }
    }
    private void Loadshendetails(){
        String url = ApiManager.USERS_OPENSTORE_INDEX;

        android.util.Log.e("数据", "进入--" + url);
        HttpUtils http = new HttpUtils();
        //http://www.jhjvip.cn:8081/market/mobile/jd/index?
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
//                  testTextView.setText(current + "/" + total);
                Log.e("数据", "数据-------" + current + "/" + total);
            }

            //
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
//                    textView.setText(responseInfo.result);
                try {
                    Log.e("数据123", "数据d-------" + responseInfo.result);
                    Gson gson = new Gson();
                    TribeDetailsBean list = gson.fromJson(responseInfo.result, TribeDetailsBean.class);
                    if(list.getCode().equals("10000")) {
                        UtilsImage.uploadimageview(iv_tribe_image,list.getData().getBanner(),"",getActivity());
                        TribeFragmentListviewAdapter adapter=new TribeFragmentListviewAdapter(getActivity(),list.getData().getList());
                        mylv_tribe_title.setAdapter(adapter);
                    }else{
                        Toast.makeText(getActivity(),list.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                }catch (JsonSyntaxException e){
                    Toast.makeText(getActivity(),"未知错误",Toast.LENGTH_LONG).show();
                }

            }

            @Override
            public void onStart() {
            }

            @Override
            public void onFailure(HttpException error, String msg) {
            }
        });
    }
    private void Loadshenqing(){
        String url = ApiManager.USERS_OPENSTORE_RUZHU+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)
                +"&appId="+ AUniqueIdentifier.getDeviceId(getActivity())+"&phone="+et_tribe_phone.getText().toString().trim()
                +"&weixinAccount="+et_tribe_weixinhao.getText().toString().trim()
                +"&inviteCode="+et_tribe_InvitateCode.getText().toString().trim();

        android.util.Log.e("数据", "进入--" + url);
        HttpUtils http = new HttpUtils();
        //http://www.jhjvip.cn:8081/market/mobile/jd/index?
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
//                  testTextView.setText(current + "/" + total);
                Log.e("数据", "数据-------" + current + "/" + total);
            }

            //
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
//                    textView.setText(responseInfo.result);
                try {
                    Log.e("数据123", "数据d-------" + responseInfo.result);
                    JSONObject obj=new JSONObject(responseInfo.result);
                    if(obj.getString("code").equals("10000")) {
                        Toast.makeText(getActivity(),obj.getString("msg"),Toast.LENGTH_SHORT).show();
                        centerDialog.dismiss();
                        centerDialogTrue.show();

                    }else{
                        Toast.makeText(getActivity(),obj.getString("msg"),Toast.LENGTH_SHORT).show();
                    }
                }catch (JsonSyntaxException e){
                    Toast.makeText(getActivity(),"未知错误",Toast.LENGTH_LONG).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart() {
            }

            @Override
            public void onFailure(HttpException error, String msg) {
            }
        });
    }
    private void Loadkaidian(){
        String url = ApiManager.USERS_OPENSTO+"?token="+ SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN)+"&appId="+ AUniqueIdentifier.getDeviceId(getActivity());

        android.util.Log.e("数据", "进入--" + url);
        HttpUtils http = new HttpUtils();
        //http://www.jhjvip.cn:8081/market/mobile/jd/index?
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {
            @Override
            public void onLoading(long total, long current, boolean isUploading) {
//                  testTextView.setText(current + "/" + total);
                Log.e("数据", "数据-------" + current + "/" + total);
            }

            //
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
//                    textView.setText(responseInfo.result);
                try {
                    Log.e("数据123", "数据d-------" + responseInfo.result);
                    JSONObject obj=new JSONObject(responseInfo.result);
                    if(obj.getString("code").equals("10000")) {
                        centerDialog.show();
                    }else if(obj.getString("code").equals("10001")) {
                        Intent intent=new Intent(getActivity(),LoginActivity.class);
                        startActivity(intent);
                    }else if(obj.getString("code").equals("10077")){
                        centerDialogTrueIsYunDianZhu.show();
                        tv_fragment_ruzhu.setText("已是云店主");
                    }else if(obj.getString("code").equals("10080")){
                        tv_fragment_ruzhu.setText("待审核");
                    }else{
                        Toast.makeText(getActivity(),obj.getString("msg"),Toast.LENGTH_SHORT).show();
                    }
                }catch (JsonSyntaxException e){
                    Toast.makeText(getActivity(),"未知错误",Toast.LENGTH_LONG).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart() {
            }

            @Override
            public void onFailure(HttpException error, String msg) {
            }
        });
    }


}
